/******************************************************************************
*  Copyright(c) 2021-2024 Sietium
*
*  @file:    maxsp-compute.cpp
*  @brif:    单精度浮点数性能
*
*  @date:    08/04/2022
*  @Author:  way.lau
*
******************************************************************************/
#include <iostream>
#include <string.h>
#include "objectfactory.h"
#include "node.h"
#include <string>
#include <vector>
#include <stdlib.h>
#include "cltkit.h"
#include "fdoutbuf.h"
#include "ig.h"
#include "stopwatch.h"

class MaxSp_compute : public Node
{
public:
    MaxSp_compute()
    {
        nodeUint = "GFLOPS";
    }
    std::string name() override
    {
        return "maxsp-compute";
    }
    std::string describe() override
    {
        return "单精度浮点数性能";
    }
    Node::NodeType  nodeType() override
    {
        return NodeTP_DataSpeed;
    }
protected:
    bool startup() override;
    bool perform() override;
    void shutdown() override;
private:
    struct cl_maxsp_t {
        cl_device_id        deviceid;
        cl_context          context;
        cl_command_queue    queue;
    } handle;
};

bool MaxSp_compute::startup()
{
    memset(&handle, 0, sizeof(cl_maxsp_t));
    cl_device_fp_config config;

    handle.deviceid = deviceinfo->deviceID();
    handle.context = clCreateContext(NULL, 1,  &handle.deviceid, NULL, NULL, NULL);
    handle.queue = clCreateCommandQueue(handle.context, handle.deviceid,
                                        CL_QUEUE_PROFILING_ENABLE,
                                        NULL);
    return true;
}

bool MaxSp_compute::perform()
{
#if (__GNUC__ > 5)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wignored-attributes"
#endif
    BasicDataPerformance<cl_float> sperf;
    nodeValue = sperf.perform(handle.deviceid, handle.context, handle.queue);
#if (__GNUC__ > 5)
#pragma GCC diagnostic pop
#endif
    return true;
}

void MaxSp_compute::shutdown()
{
    if (handle.queue) {
        CHECK_CL_ERROR(clReleaseCommandQueue(handle.queue));
        handle.queue = nullptr;
    }
    if (handle.context) {
        CHECK_CL_ERROR(clReleaseContext(handle.context));
        handle.context = nullptr;
    }
}
REGISTER_OBJECT(Node, MaxSp_compute)
